﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections;

namespace SmartMeterParamFrame.Model
{

    /// <summary>
    /// 被检表信息
    /// </summary>
    public class MeterInfo : DependencyObject
    {
        private static int bwh = 0;

        public static DependencyProperty CheckYnProperty = DependencyProperty.Register("CheckYn", typeof(bool), typeof(MeterInfo));
        public static DependencyProperty MeterAddressProperty = DependencyProperty.Register("MeterAddress", typeof(string), typeof(MeterInfo));
        public static DependencyProperty CurrentPrjNameProperty = DependencyProperty.Register("CurrentPrjName", typeof(string), typeof(MeterInfo));
        public static DependencyProperty CurrentDescProperty = DependencyProperty.Register("CurrentDesc", typeof(string), typeof(MeterInfo));
        public static DependencyProperty CurrentStateProperty = DependencyProperty.Register("CurrentState", typeof(EnumMeterState), typeof(MeterInfo));
        public static DependencyProperty CurrentSetResultProperty = DependencyProperty.Register("CurrentSetResult", typeof(EnumResult), typeof(MeterInfo));
        public static DependencyProperty CurrentCheckResultProperty = DependencyProperty.Register("CurrentCheckResult", typeof(EnumResult), typeof(MeterInfo));

        /// <summary>
        /// 重置
        /// </summary>
        public static void Reset()
        {
            bwh = 0;
        }

        public MeterInfo()
        {

            MeterBwh = ++bwh;

            MeterAddress = "".PadLeft(12, 'A');

            MControl = new MeterControl(this);

            CheckPrjResults = new Dictionary<int, ProjectCheckResult>();

        }

        public MeterControl MControl
        {
            get;
            private set;
        }

        /// <summary>
        /// 是否要检
        /// </summary>
        public bool CheckYn
        {
            get
            {
                return (bool)this.Dispatcher.Invoke(new Func<bool>(() => (bool)this.GetValue(CheckYnProperty)));
            }
            set
            {
                //this.CurrentState = value ? EnumMeterState.进行 : EnumMeterState.不检;
                this.SetValue(CheckYnProperty, value);
            }
        }

        /// <summary>
        /// 电能表最终操作的结果
        /// </summary>
        public EnumResult OperationResult
        {
            get
            {
                ProjectCheckResult tmpvalue = new ProjectCheckResult(EnumResult.未操作, EnumResult.未操作);
                foreach (ProjectCheckResult result in CheckPrjResults.Values)
                {
                    tmpvalue = result;

                    if (tmpvalue.CheckResult == EnumResult.失败) return EnumResult.失败;
                }

                if (tmpvalue.CheckResult == EnumResult.成功) return EnumResult.成功;

                return EnumResult.未操作;
            }
        }


        /// <summary>
        /// 当前方案项目已经检定完成
        /// </summary>
        /// <param name="PrjIndex"></param>
        /// <returns></returns>
        public bool IsFaItemOk(int PrjIndex)
        {
            if (this.CheckYn && CheckPrjResults.ContainsKey(PrjIndex))
            {
                if (CheckPrjResults[PrjIndex].CheckResult == EnumResult.成功) return true;
            }
            return false;
        }
        /// <summary>
        /// 表位号
        /// </summary>
        public int MeterBwh
        {
            get;
            private set;
        }
        /// <summary>
        /// 表通信地址
        /// </summary>
        public string MeterAddress
        {
            get
            {
                return (string)this.Dispatcher.Invoke(new Func<string>(() => (string)this.GetValue(MeterAddressProperty)));
            }
            set
            {
                this.Dispatcher.Invoke(new Action<string>((string s) => this.SetValue(MeterAddressProperty, s)), value);
            }
        }

        ///// <summary>
        ///// 当前项目
        ///// </summary>
        //public string CurrentPrjName
        //{
        //    get { return (string)this.GetValue(CurrentPrjNameProperty); }
        //    set { this.Dispatcher.Invoke(new Action(() => this.SetValue(CurrentPrjNameProperty, value))); }
        //}
        /// <summary>
        /// 当前项目工作描述
        /// </summary>
        public string CurrentDesc
        {
            get
            {
                return (string)this.Dispatcher.Invoke(new Func<string>(() => (string)this.GetValue(CurrentDescProperty)));
            }
            set
            {
                if (MeterControl.IsAsynchronous == false)
                    this.Dispatcher.Invoke(new Action<string>((string s) => this.SetValue(CurrentDescProperty, s)), value);
                else
                    this.Dispatcher.BeginInvoke(new Action<string>((string s) => this.SetValue(CurrentDescProperty, s)), value);
            }
        }

        /// <summary>
        /// 当前项目工作状态
        /// </summary>
        public EnumMeterState CurrentState
        {
            get { return (EnumMeterState)this.GetValue(CurrentStateProperty); }
            private set { this.SetValue(CurrentStateProperty, value); }
        }

        /// <summary>
        /// 当前项目设置结果
        /// </summary>
        public EnumResult CurrentSetResult
        {
            get { return (EnumResult)this.GetValue(CurrentSetResultProperty); }
            private set
            {
                this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.SetValue(CurrentSetResultProperty, value);
                        switch (value)
                        {
                            //case EnumResult.成功:
                            case EnumResult.进行中:
                                this.CurrentState = EnumMeterState.进行;
                                break;
                            case EnumResult.未操作:
                                this.CurrentState = EnumMeterState.不检;
                                break;
                            case EnumResult.失败:
                                this.CurrentState = EnumMeterState.失败;
                                break;
                        }
                    }));
            }
        }
        /// <summary>
        /// 当前项目复核结果
        /// </summary>
        public EnumResult CurrentCheckResult
        {
            get { return (EnumResult)this.GetValue(CurrentCheckResultProperty); }
            private set
            {
                this.Dispatcher.BeginInvoke(new Action(() =>
                                                    {

                                                        switch (value)
                                                        {
                                                            case EnumResult.成功:
                                                                this.CurrentState = EnumMeterState.成功;
                                                                break;
                                                            case EnumResult.进行中:
                                                                this.CurrentState = EnumMeterState.进行;
                                                                break;
                                                            case EnumResult.失败:
                                                                this.CurrentState = EnumMeterState.失败;
                                                                break;
                                                            //default:
                                                            //    this.CurrentState = EnumMeterState.不检;
                                                            //    break;
                                                        }

                                                        this.SetValue(CurrentCheckResultProperty, value);
                                                    }));
            }
        }

        /// <summary>
        /// 获取项目结论
        /// </summary>
        /// <param name="PrjIndex">项目ID</param>
        /// <param name="IsCheck">是否是核验结论</param>
        /// <returns></returns>
        public EnumResult getPrjResult(int PrjIndex, bool IsCheck)
        {
            if (!CheckPrjResults.ContainsKey(PrjIndex)) return EnumResult.未操作;

            ProjectCheckResult item = CheckPrjResults[PrjIndex];

            return IsCheck ? item.CheckResult : item.SetResult;
        }

        /// <summary>
        /// 设置当前项目结论
        /// </summary>
        /// <param name="PrjIndex">项目ID</param>
        public void SetCurrentPrjResult(int PrjIndex)
        {
            ProjectCheckResult item;

            if (CheckPrjResults.ContainsKey(PrjIndex))
            {
                item = CheckPrjResults[PrjIndex];
            }
            else
            {
                item = new ProjectCheckResult(EnumResult.未操作, EnumResult.未操作);
            }

            CurrentCheckResult = item.CheckResult;
            CurrentSetResult = item.SetResult;

        }

        /// <summary>
        /// 设置项目结论
        /// </summary>
        /// <param name="PrjIndex">项目ID</param>
        /// <param name="result">结论</param>
        /// <param name="IsCheck">是不是复核结论</param>
        public void SetPrjResult(int PrjIndex, EnumResult result, bool IsCheck)
        {
            if (CheckPrjResults.ContainsKey(PrjIndex))
            {
                if (IsCheck)
                {
                    CheckPrjResults[PrjIndex].CheckResult = result;
                }
                else
                {
                    CheckPrjResults[PrjIndex].SetResult = result;
                }
            }
            else
            {
                CheckPrjResults.Add(PrjIndex, new ProjectCheckResult(IsCheck ? EnumResult.未操作 : result, IsCheck ? result : EnumResult.未操作));
            }

            if (IsCheck)
            {
                CurrentCheckResult = result;
            }
            else
            {
                CurrentSetResult = result;
            }
        }

        /// <summary>
        /// 设置项目结论
        /// </summary>
        /// <param name="PrjInex">项目ID</param>
        /// <param name="setResult">设置结论</param>
        /// <param name="checkResult">复核结论</param>
        public void SetPrjResult(int PrjInex, EnumResult setResult, EnumResult checkResult)
        {
            if (CheckPrjResults.ContainsKey(PrjInex))
            {
                CheckPrjResults[PrjInex].CheckResult = checkResult;
                CheckPrjResults[PrjInex].SetResult = setResult;
            }
            else
            {
                ProjectCheckResult item = new ProjectCheckResult(setResult, checkResult);
                CheckPrjResults.Add(PrjInex, item);
            }

            CurrentCheckResult = checkResult;

            CurrentSetResult = setResult;

        }
        /// <summary>
        /// 项目结论集合数量
        /// </summary>
        public int PrjResultsCount
        {
            get { return CheckPrjResults.Count; }
        }

        /// <summary>
        /// 当前表项目检定结果集
        /// </summary>
        private Dictionary<int, ProjectCheckResult> CheckPrjResults;

        /// <summary>
        /// 是否是存在失败项目
        /// </summary>
        public bool IsFailed
        {
            get
            {
                foreach (ProjectCheckResult item in CheckPrjResults.Values)
                {
                    if (item.CheckResult == EnumResult.失败)
                    { return true; }
                }
                return false;
            }
        }

        /// <summary>
        /// 将所有失败的数据重新设置成 未执行
        /// </summary>
        public void ResetAllUnSuccessToNone()
        {
            foreach (ProjectCheckResult item in CheckPrjResults.Values)
            {
                if (item.CheckResult != EnumResult.成功)
                {
                    item.CheckResult = EnumResult.未操作;
                    item.SetResult = EnumResult.未操作;
                    break;
                }
            }
        }

        /// <summary>
        /// 清空所有已有的结论
        /// </summary>
        public void ClearAllCheckResult()
        {
            this.CheckPrjResults.Clear();
        }
    }

    /// <summary>
    /// 项目结果
    /// </summary>
    public class ProjectCheckResult
    {
        /// <summary>
        /// 项目设置结果
        /// </summary>
        public EnumResult SetResult
        {
            get;
            internal set;
        }
        /// <summary>
        /// 项目复核结果
        /// </summary>
        public EnumResult CheckResult
        {
            get;
            internal set;
        }

        public ProjectCheckResult(EnumResult setResult, EnumResult checkResult)
        {
            SetResult = setResult;
            CheckResult = checkResult;
        }
    }
}
